home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 October / enter-2005-10.iso / files / jedit42install.exe / {app} / macros / Editing / Mode_Switcher.bsh < prev    next >
Encoding:
Text File  |  2004-08-29  |  5.8 KB  |  188 lines

  1. /*
  2.  * ModeSwitcher.bsh - a BeanShell macro script for changing the current
  3.  * buffer's edit mode.
  4.  *
  5.  * Copyright (C) 2004 Nicholas O'Leary nol@deferential.net
  6.  * 
  7.  * :mode=beanshell:tabSize=3:indentSize=3:maxLineLen=0:noTabs=false:
  8.  * :indentOnTab=true:indentOnEnter=true:folding=explicit:collapseFolds=1:
  9.  *
  10.  * {{{ License
  11.  * This program is free software; you can redistribute it and/or
  12.  * modify it under the terms of the GNU General Public License
  13.  * as published by the Free Software Foundation; either version 2
  14.  * of the License, or any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with the jEdit program; if not, write to the Free Software
  23.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  24.  * }}}
  25.  *
  26.  * Notes:
  27.  *  There are two other ways to change the buffers mode:
  28.  *    - enter 'buffer.mode=[mode]' in the action bar
  29.  *    - change it in the Buffer Options dialog
  30.  *  Whilst both of these do the job, I wanted a way to achieve it with minimum
  31.  *  effort, and keypresses.
  32.  *  It also has the benefit of auto-completion of mode names.
  33.  * 
  34.  * $Id: Mode_Switcher.bsh,v 1.1 2004/04/28 21:15:29 spestov Exp $
  35.  */
  36.  
  37. import javax.swing.border.EmptyBorder;
  38.  
  39. Mode[] modes = jEdit.getModes();
  40. JDialog dialog = new JDialog(view, "Buffer Mode Switcher", true);
  41. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  42. JPanel content = new JPanel(new BorderLayout());
  43. content.setBorder(new EmptyBorder(12,12,12,12));
  44. Mode[] modes = jEdit.getModes();
  45. String[] names = new String[modes.length];
  46. for(int i=0;i<modes.length;i++) {
  47.     names[i] = modes[i].getName();
  48. }
  49. Arrays.sort(names);
  50. JTextField textfield = new JTextField() {
  51.     protected String[] names;
  52.     protected boolean shifted = false;
  53.     //{{{ setNames
  54.     public void setNames(String[] a){
  55.         names = a;
  56.     } //}}}
  57.     public boolean getFocusTraversalKeysEnabled(){return false;}
  58.     //{{{ processKeyEvent
  59.     protected void processKeyEvent(KeyEvent evt)
  60.     {
  61.         if (evt.getID() == KeyEvent.KEY_RELEASED) {
  62.             if (evt.getKeyCode() == KeyEvent.VK_SHIFT) {
  63.                 shifted = false;
  64.             }
  65.         } else if(evt.getID() == KeyEvent.KEY_PRESSED) {
  66.             if (evt.getKeyCode() == KeyEvent.VK_SHIFT) {
  67.                 shifted = true;
  68.             } else if(evt.getKeyCode() == KeyEvent.VK_TAB) {
  69.                 // Get the current text
  70.                 String txt = getText();
  71.                 String original = txt;
  72.                 // See if some text is selected
  73.                 if (getSelectedText() != null)
  74.                     txt = txt.substring(0,txt.length()-getSelectedText().length());
  75.                 // txt represents the unhighlighted text in the box. This is used
  76.                 // to find further matches.
  77.                 
  78.                 // See if the current text is a known mode
  79.                 int index = Arrays.binarySearch(names,original);
  80.                 if (index < 0) index = 0;
  81.                 int indexStep = 1;
  82.                 if (shifted) indexStep = -1;
  83.                 index+=indexStep;
  84.                 if (index == names.length) index = 0;
  85.                 if (index < 0) index = names.length-1;
  86.                 int match = -1;
  87.                 boolean foundExact = false;
  88.                 boolean keepLooping = true;
  89.                 // Loop through modes, starting at current+1
  90.                 int i = index;
  91.                 while(keepLooping) {
  92.                     // Skip if the mode name is shorter than the current text
  93.                     if (names[i].length()>=txt.length())
  94.                     {
  95.                         // If the mode matches, escape
  96.                         if (names[i].substring(0,txt.length()).equals(txt)) {
  97.                             match = i;
  98.                             break;
  99.                         }
  100.                     }
  101.                     // Loop the loop
  102.                     i+=indexStep;
  103.                     if (i == names.length) i = 0;
  104.                     if (i < 0) i = names.length-1;
  105.                     if (i==index) break;
  106.                 }
  107.                 // If a match has been found...
  108.                 if (match >= 0) {
  109.                     setText(names[match]);
  110.                     setSelectionStart(txt.length());
  111.                     setSelectionEnd(names[match].length());
  112.                 }
  113.                 return;
  114.             }
  115.         }
  116.         super.processKeyEvent(evt);
  117.     } //}}}
  118. };
  119. textfield.setColumns(20);
  120. textfield.setNames(names);
  121. Mode m = buffer.getMode();
  122. // Set the inital text to the current mode, and highlight it, so a key
  123. // press will clear the entry.
  124. if (m != null) {
  125.     textfield.setText(m.getName());
  126.     textfield.setSelectionStart(0);
  127.     textfield.setSelectionEnd(m.getName().length());
  128. }
  129. content.add(new JLabel("Enter buffer mode:"), BorderLayout.NORTH);
  130. content.add(textfield, BorderLayout.CENTER);
  131. Vector v = new Vector();
  132.  
  133. // KeyListener Interface
  134. //{{{ keyPressed
  135. void keyPressed(evt)
  136. {
  137.     if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
  138.         dialog.dispose();
  139.     else if(evt.getKeyCode() == KeyEvent.VK_ENTER)
  140.     {
  141.         Mode m = jEdit.getMode(textfield.getText());
  142.         if (m!=null)
  143.         {
  144.             buffer.setMode(m);
  145.             Log.log(Log.NOTICE,
  146.                       BeanShell.class,
  147.                       "Changing mode of buffer ["+
  148.                           buffer.getName()+"] to ["+
  149.                           m.getName()+"]");
  150.         } else {
  151.             Log.log(Log.WARNING,
  152.                       BeanShell.class,
  153.                       "Mode ["+textfield.getText()+"] not found");
  154.         }
  155.         evt.consume();
  156.         dialog.dispose();
  157.     }
  158. }//}}}
  159. void keyReleased(evt) {}
  160. void keyTyped(evt) {}
  161.  
  162. dialog.addKeyListener(this);
  163. textfield.addKeyListener(this);
  164. dialog.setContentPane(content);
  165. dialog.pack();
  166. dialog.setLocationRelativeTo(view);
  167. dialog.setVisible(true);
  168.  
  169. /*
  170.  
  171. Macro index data (in DocBook format)
  172.  
  173.    <listitem>
  174.       <para><filename>Mode_Switcher.bsh</filename></para>
  175.       Displays a modal dialog with the current buffer's mode in a text field,
  176.       allowing one to change the mode by typing in its name.
  177.       <keycap>ENTER</keycap> selects the current mode; if the text is not a 
  178.       valid mode, the dialog still dismisses, but a warning is logged to the
  179.       activity log.
  180.       <keycap>ESACPE</keycap> closes the dialog with no further action.
  181.       <keycap>TAB</keycap> attempts to auto-complete the mode name. Pressing
  182.       <keycap>TAB</keycap> repeatedly cycles through the possible completions. 
  183.         <keycap>SHIFT-TAB</keycap> cycles through the completions in reverse.
  184.       </para></abstract>
  185.    </listitem>
  186.  
  187. */
  188.